package com.pn.service.impl;

import com.pn.entity.purchaseAmount;
import com.pn.mapper.InstoreMapper;
import com.pn.mapper.OutstoreMapper;
import com.pn.mapper.PurchaseMapper;
import com.pn.mapper.StatisticsMapper;
import com.pn.service.StatisticsService;
import com.pn.vo.InStoreAndOutStoreVo;
import com.pn.vo.StoreCountVo;
import com.pn.vo.purchaseAmountVo;
import com.pn.vo.todayDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private StatisticsMapper statisticsMapper;

    //统计仓库库存
    public List<StoreCountVo> queryStoreCount(){
        return statisticsMapper.findStoreCount();
    }

    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private InstoreMapper instoreMapper;
    @Autowired
    private OutstoreMapper outstoreMapper;

    //今日数据
    @Override
    public todayDataVo todayData(LocalDateTime begin, LocalDateTime end) {
        Integer purchaseNum = purchaseMapper.todayPurchase(begin, end);
        if (purchaseNum == null){
            purchaseNum = 0;
        }
        Integer inStoreNum = instoreMapper.todayInStoreNum(begin, end);
        if (inStoreNum == null){
            inStoreNum = 0;
        }
        Integer outStoreNum = outstoreMapper.todayOutStoreNum(begin, end);
        if (outStoreNum == null){
            outStoreNum = 0;
        }
        todayDataVo todayDataVo = new todayDataVo();
        todayDataVo.setPurchaseNum(purchaseNum);
        todayDataVo.setInStoreNum(inStoreNum);
        todayDataVo.setOutStoreNum(outStoreNum);
        return todayDataVo;
    }

    //采购占比
    @Override
    public List<purchaseAmount> purchaseAmount() {
        List<purchaseAmount> list =  statisticsMapper.purchaseAmount();
        return list;
    }

    //2024年出入库信息
    @Override
    public InStoreAndOutStoreVo InStoreAndOutStore() {

        ArrayList<Integer> inStoreNumList = new ArrayList<>();
        ArrayList<Integer> outStoreNumList = new ArrayList<>();

        // 获取当前年份
        int currentYear = LocalDate.now().getYear();

        // 创建DateTimeFormatter来定义所需的格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 循环遍历每个月
        for (int month = 1; month <= 12; month++) {
            // 构建当前年月的YearMonth对象
            YearMonth yearMonth = YearMonth.of(currentYear, month);

            // 获取该月的第一天
            LocalDate firstDay = yearMonth.atDay(1);
            // 获取该月的最后一天
            LocalDate lastDay = yearMonth.atEndOfMonth();

            // 获取该月最后一天的最后一秒
            LocalDateTime lastDayEnd = lastDay.atStartOfDay().plusDays(1).minusSeconds(1);

            // 格式化日期为yyyy-MM-dd HH:mm:ss
            String begin = firstDay.atStartOfDay().format(formatter);
            String end = lastDayEnd.format(formatter);

            Integer inStoreNum = instoreMapper.monthInstoreNum(begin,end);
            Integer outStoreNum = outstoreMapper.monthOutstoreNum(begin,end);


            inStoreNumList.add(inStoreNum==null?0:inStoreNum);
            outStoreNumList.add(outStoreNum==null?0:outStoreNum);

        }
        InStoreAndOutStoreVo inStoreAndOutStoreVo = new InStoreAndOutStoreVo();
        inStoreAndOutStoreVo.setInStoreNumList(inStoreNumList);
        inStoreAndOutStoreVo.setOutStoreNumList(outStoreNumList);

        return inStoreAndOutStoreVo;
    }
}
